﻿/*
 * Author: Duncan Jenkins
 * 
 * Description: Turns a image into binary format, white and black, based on entered threshold
 * */

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;

namespace LibraryCS {
    public class Binarize : VisionAlgorithm {
        private int Threshold = 100;

        //private Bitmap SourceBitmap, OutputBitmap;
        private VImage SourceImage, OutputImage;


        public Binarize(String name) : base(name) { }

        public VImage BinarizeBitmap(Bitmap InputBitmap) {
            SourceImage = new VImage(InputBitmap);
            OutputImage = new VImage(SourceImage.Width, SourceImage.Height);
            DoBinarize();

            return OutputImage;
        }

        public VImage BinarizeImage(VImage InputImage) {
            SourceImage = InputImage;
            OutputImage = new VImage(SourceImage.Width, SourceImage.Height);
            DoBinarize();

            return OutputImage;
        }

        public override Bitmap DoAlgorithm(Bitmap sourceImage) {
            throw new NotImplementedException();
        }

        public void DoBinarize() {

            // Threshold the image
            for (int i = 0; i < SourceImage.GrayPixels.Length; i++) {
                if (SourceImage.GrayPixels[i] > Threshold) {
                    // Part of the foreground, make it white
                    OutputImage.RedPixels[i] = 255;
                    OutputImage.GreenPixels[i] = 255;
                    OutputImage.BluePixels[i] = 255;
                } else {
                    // Part of the background, leave it black
                    OutputImage.RedPixels[i] = 0;
                    OutputImage.GreenPixels[i] = 0;
                    OutputImage.BluePixels[i] = 0;
                }
            }
            return;
        }

        public static int[] binarizeToSingleInt(VImage sourceImage) {
            int[] output = new int[sourceImage.GrayPixels.Length];
            for (int x = 0; x < sourceImage.GrayPixels.Length; x++) {
                if (sourceImage.GrayPixels[x] < 100) {
                    // Black is foreground
                    output[x] = 1;
                } else {
                    output[x] = 0;
                }
            }
            return output;
        }

        #region GUI Functions

        public override SettingsPropertyBox createControls() {
            SettingsPropertyBox Controls = new SettingsPropertyBox();

            // Create a Label indictating the type of setting this is
            Label lbThreshold = new Label();
            lbThreshold.AutoSize = true;
            lbThreshold.Text = "Threshold";
            lbThreshold.Name = "lbThreshold";

            // Create a TextBox Control for setting the percentage of noise
            TextBox tbThreshold = new TextBox();
            tbThreshold.Text = Threshold.ToString();
            tbThreshold.Name = "tbThreshold";
            tbThreshold.AcceptsReturn = false;
            tbThreshold.MaxLength = 3;
            tbThreshold.Multiline = false;
            tbThreshold.Size = new Size(50, 25);

            // Add the controls to the list
            //Controls.Add(tbThreshold);
            //Controls.Add(lbThreshold);
            Controls.AddLine(tbThreshold, lbThreshold, null, null);
            // Return the list
            return Controls;
        }

        public override void setSettings(System.Collections.Generic.List<object> settings) {
            try {
                Threshold = Int32.Parse((string)settings[0]);

                Debug.WriteLine("Settings entered: " + Threshold);
            } catch (FormatException) {
                MessageBox.Show("Wrong Format has been entered.\nPlease enter integers\nNew Settings have been discarded for " + Name, "Format Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);
            }
        }

        #endregion
    }
}
